package com.rzhd.coursepatriarch.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rzhd.coursepatriarch.common.R;
import com.rzhd.coursepatriarch.common.listener.ToolbarLeftOrRightClickListener;

/**
 * @author yql
 * 2018.4.22
 */

public class CustomToolbar extends RelativeLayout {

    private Context context;

    private LayoutInflater inflater;

    /**
     * 默认背景颜色
     */
    private int layoutBackgroundColor = 0;

    private RelativeLayout toolbarLayout;
    /**
     * 左边按钮
     */
    private ImageView returnBtn;
    /**
     * 一般网页页面才使用
     */
    private ImageView closeBtn;
    /**
     * 默认toolbar中部文字
     */
    private TextView mTitleDefault;
    /**
     * 默认toolbar右侧文字
     */
    private TextView mToolbarRightText;
    /*
     * 中部自定义父布局
     */
    private FrameLayout mToolbarFrameLayout;
    /**
     * 自定义中部（含左右功能的textView）
     */
    private TextView mTitleLeft, mTitleRight;
    /**
     * 自定义中部tableLayout
     */
    private TabLayout tabLayout;

    /*----------------------------------------------------*/

    /**
     * 中部布局模式
     */
    private int CUSTOM_CENTER_TYPE = CENTER_TYPE_DEFAULT;
    /**
     * 默认模式（textView）
     */
    public static final int CENTER_TYPE_DEFAULT = 0;
    /**
     * 含左右功能的textView模式
     */
    public static final int CENTER_TYPE_LEFT_OF_RIGHT = 1;
    /**
     * tabLayout模式
     */
    public static final int CENTER_TYPE_TAB_LAYOUT = 2;
    /**
     *
     */
    private static final int CENTER_TYPE_CUSTOM = 3;

    private int titleTextColor = 0;
    /**
     * 选中状态下的字体颜色
     */
    private int selectTextColor = 0;
    /**
     * 非选中状态下的字体颜色
     */
    private int normalTextColor = 0;
    /**
     * 选中状态下的背景色
     */
    private Drawable selectBackground;
    /**
     * 正常状态下的背景色
     */
    private Drawable normalBackground;

    private ToolbarLeftOrRightClickListener toolbarLeftOrRightClickListener;

    /**
     * 是否显示顶部view
     */
    private boolean isShowTopView;
    private View topView;

    public void setOnToolbarClickListener(ToolbarLeftOrRightClickListener listener) {
        this.toolbarLeftOrRightClickListener = listener;
    }

    public CustomToolbar(Context context) {
        this(context, null);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initCustomAttrs(context, attrs);
        initView();
    }

    private void initCustomAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToolbar);
        CUSTOM_CENTER_TYPE = typedArray.getInt(R.styleable.CustomToolbar_custom_center_type, CENTER_TYPE_DEFAULT);
        selectTextColor = typedArray.getColor(R.styleable.CustomToolbar_select_text_color, Color.WHITE);
        normalTextColor = typedArray.getColor(R.styleable.CustomToolbar_normal_text_color, Color.RED);
        normalTextColor = typedArray.getColor(R.styleable.CustomToolbar_title_text_color, Color.BLACK);
        selectBackground = typedArray.getDrawable(R.styleable.CustomToolbar_select_background);
        normalBackground = typedArray.getDrawable(R.styleable.CustomToolbar_normal_background);
        typedArray.recycle();
    }

    /**
     * 初始化view
     */
    private void initView() {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.view_custom_toolbar, this);
        toolbarLayout = view.findViewById(R.id.toolbar_layout);
        toolbarLayout.setBackgroundColor(layoutBackgroundColor);
        returnBtn = view.findViewById(R.id.returnBtn);
        //===================2019-10-12新增================
        closeBtn = view.findViewById(R.id.closeBtn);
        //===================2019-10-12新增================
        topView = view.findViewById( R.id.custom_toolbar_top_view );
        mTitleDefault = view.findViewById(R.id.mToolbarTitle);
        mToolbarRightText = view.findViewById(R.id.mToolbarRightText);
        mToolbarFrameLayout = view.findViewById(R.id.mToolbarFrameLayout);
        setCUSTOM_CENTER_TYPE(CUSTOM_CENTER_TYPE);
    }

    /**
     * 设置标题栏模式
     *
     * @param CUSTOM_CENTER_TYPE 标题栏属性
     */
    public void setCUSTOM_CENTER_TYPE(int CUSTOM_CENTER_TYPE) {
        this.CUSTOM_CENTER_TYPE = CUSTOM_CENTER_TYPE;
        switch (CUSTOM_CENTER_TYPE) {
            //默认模式
            case CENTER_TYPE_DEFAULT:
                mToolbarFrameLayout.setVisibility(GONE);
                break;
            //左右toolbar
            case CENTER_TYPE_LEFT_OF_RIGHT:
//                mTitleDefault.setVisibility(GONE);
//                mToolbarFrameLayout.setVisibility(VISIBLE);
//                initLeftOrRight();
                break;
            //tabLayout
            case CENTER_TYPE_TAB_LAYOUT:
                mTitleDefault.setVisibility(GONE);
                mToolbarFrameLayout.setVisibility(VISIBLE);
                initTabLayout();
                break;
            //自己定义
            case CENTER_TYPE_CUSTOM:
                mTitleDefault.setVisibility(GONE);
                mToolbarFrameLayout.setVisibility(VISIBLE);
                break;
            default:
                break;
        }
    }

    /**
     * 初始化 自定义 左右title toolbar
     */
    private void initLeftOrRight() {
//        View view = inflater.inflate(R.layout.view_toolbar_left_right, null);
//        mTitleLeft = view.findViewById(R.id.left);
//        mTitleRight = view.findViewById(R.id.right);
//        if (mToolbarFrameLayout != null) {
//            mToolbarFrameLayout.addView(view);
//        }
        setSelectLeftOrRight(true);
    }

    /**
     * 初始化TabLayout toolbar
     */
    private void initTabLayout() {
        View view = inflater.inflate(R.layout.view_toolbar_tab_layout, null);
        tabLayout = view.findViewById(R.id.tabLayout);
        if (mToolbarFrameLayout != null) {
            mToolbarFrameLayout.addView(view);
        }
    }

    /**
     * 设置默认title名字
     *
     * @param toolbarName 标题名
     */
    public void setToolbarDefault(String toolbarName) {
        setToolbarDefault(toolbarName, "");
    }

    public void setToolbarDefault(String toolbarName , boolean isShowTopView ) {
        setToolbarDefault(toolbarName, "" , isShowTopView );
    }

    /**
     * 设置默认title是否加粗
     */
    public void setToolbarDefaultBold(boolean isBold) {
        if (isBold) {
            mTitleDefault.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mTitleDefault.setTypeface(Typeface.DEFAULT);
        }
    }

    /**
     * 设置默认title名字 颜色
     */
    public void setToolbarDefaultColor(int color) {
        mTitleDefault.setTextColor(color);
    }

    /**
     * 设置默认title名字（含有右侧文字）
     *
     * @param toolbarName 标题名
     * @param rightText   右侧文字信息
     */
    public void setToolbarDefault(String toolbarName, String rightText) {
        if (mTitleDefault == null || mToolbarRightText == null) {
            return;
        }
        setToolbarName(toolbarName);
        setRightName(rightText);
    }

    public void setToolbarDefault(String toolbarName, String rightText , boolean isShowTopView ) {
        if (mTitleDefault == null || mToolbarRightText == null) {
            return;
        }
        setToolbarName(toolbarName);
        setRightName(rightText);
        setShowTopView( isShowTopView );
    }



    public void setShowTopView(boolean showTopView) {
        isShowTopView = showTopView;
        if( isShowTopView ){
            topView.setVisibility( VISIBLE );
        }else{
            topView.setVisibility( GONE );
        }
    }

    /**
     * 设置默认title名字（含有右侧图片）
     *
     * @param toolbarName 标题名
     * @param resourceId  图片资源id
     */
    public void setToolbarDefault(String toolbarName, int resourceId) {
        if (mTitleDefault == null || mToolbarRightText == null) {
            return;
        }
        setToolbarName(toolbarName);
        setRightResourceId(resourceId);
    }

    /**
     * 自定义 左右Title的Toolbar（含左右功能）
     *
     * @param leftTitle  左侧文字
     * @param rightTitle 右侧文字
     */
    public void setToolbarLeftOrRight(String leftTitle, String rightTitle) {
        setToolbarLeftOrRight(leftTitle, rightTitle, "");
    }

    /**
     * 自定义 左右Title的Toolbar（含左右功能）
     *
     * @param leftTitle  左侧文字
     * @param rightTitle 右侧文字
     * @param rightText  toolbar右侧文字
     */
    public void setToolbarLeftOrRight(String leftTitle, String rightTitle, String rightText) {
        setLeftOrRightName(leftTitle, rightTitle);
        setRightName(rightText);
    }

    /**
     * 自定义 左右Title的Toolbar（含左右功能）
     *
     * @param leftTitle  左侧文字
     * @param rightTitle 右侧文字
     * @param resourceId toolbar右侧资源id
     */
    public void setToolbarLeftOrRight(String leftTitle, String rightTitle, int resourceId) {
        setLeftOrRightName(leftTitle, rightTitle);
        setRightResourceId(resourceId);
    }

    /**
     * 是否选中左侧文字
     *
     * @param selectLeft 选中
     */
    private void setSelectLeftOrRight(boolean selectLeft) {
        if (mTitleLeft == null || mTitleRight == null || selectTextColor == 0 || normalTextColor == 0
                || selectBackground == null || normalBackground == null) {
            return;
        }
        if (selectLeft) {
            mTitleLeft.setEnabled(false);
            mTitleLeft.setTextColor(getResources().getColor(selectTextColor));
            mTitleLeft.setBackground(selectBackground);
            mTitleRight.setEnabled(true);
            mTitleRight.setTextColor(getResources().getColor(normalTextColor));
            mTitleRight.setBackground(normalBackground);
        } else {
            mTitleLeft.setEnabled(true);
            mTitleLeft.setTextColor(getResources().getColor(normalTextColor));
            mTitleLeft.setBackground(normalBackground);
            mTitleRight.setEnabled(false);
            mTitleRight.setTextColor(getResources().getColor(selectTextColor));
            mTitleRight.setBackground(selectBackground);
        }
    }

    /**
     * 设置标题名
     *
     * @param toolbarName 标题名
     */
    private void setToolbarName(String toolbarName) {
        if (!TextUtils.isEmpty(toolbarName) ) {
            if( toolbarName.length() <= 10 ){
                mTitleDefault.setText(toolbarName);
            }else{
                mTitleDefault.setText(toolbarName.substring( 0 , 10 ) + "...");
            }
        }
    }

    /**
     * 设置右侧文字内容
     *
     * @param rightText 文字信息
     */
    private void setRightName(String rightText) {
        if (TextUtils.isEmpty(rightText)) {
            mToolbarRightText.setVisibility(INVISIBLE);
        } else {
            mToolbarRightText.setVisibility(VISIBLE);
            mToolbarRightText.setText(rightText);
        }
    }

    /**
     * 设置右侧文字名字 颜色
     * @param content
     * @param color
     */
    public void setmToolbarRightText(String content,int color){
        mToolbarRightText.setVisibility(VISIBLE);
        mToolbarRightText.setText(content);
        mToolbarRightText.setTextColor(color);
    }

    /**
     * 设置右侧文字内容
     *
     * @param color 颜色
     */
    public void setRightColor(int color) {
       try {
           mToolbarRightText.setVisibility(VISIBLE);
           mToolbarRightText.setTextColor(color);
        } catch (Exception e){
           mToolbarRightText.setVisibility(INVISIBLE);

        }
    }

    /**
     * 设置右侧资源文件id
     *
     * @param resourceId 资源id
     */
    public void setRightResourceId(int resourceId) {
        if (mToolbarRightText == null) {
            return;
        }
        mToolbarRightText.setVisibility(VISIBLE);
        Drawable drawable = getResources().getDrawable(resourceId);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        mToolbarRightText.setCompoundDrawables(null, null, drawable, null);
    }

    /**
     * 设置 自定义 左右Title的Toolbar名字
     *
     * @param leftName  左侧名字
     * @param rightName 右侧名字
     */
    private void setLeftOrRightName(String leftName, String rightName) {
        if (TextUtils.isEmpty(leftName) || TextUtils.isEmpty(rightName)
                || mTitleLeft == null || mTitleRight == null) {
            return;
        }
        mTitleLeft.setText(leftName);
        mTitleRight.setText(rightName);
    }

    /**
     * 设置监听
     *
     * @param listener 点击监听
     */
    public void setViewOnClickListener(OnClickListener listener) {
        if (returnBtn != null) {
            returnBtn.setOnClickListener(listener);
        }
        //==================2019-10-12新增==================
        if( closeBtn != null ){
            closeBtn.setOnClickListener(listener);
        }
        //==================2019-10-12新增==================
        if (mToolbarRightText != null) {
            mToolbarRightText.setOnClickListener(listener);
        }
        if (mTitleLeft != null) {
            mTitleLeft.setOnClickListener(listener);
        }
        if (mTitleLeft != null) {
            mTitleRight.setOnClickListener(listener);
        }
    }

    public void clickLeftToolbar() {
        setSelectLeftOrRight(true);
    }

    public void clickRightToolbar() {
        setSelectLeftOrRight(false);
    }

    public FrameLayout getToolbarFrameLayout() {
        return mToolbarFrameLayout;
    }

    public ImageView getReturnBtn() {
        return returnBtn;
    }

    public ImageView getCloseBtn(){
        return closeBtn;
    }

    public TextView getToolbarRightText() {
        return mToolbarRightText;
    }

    public int getLayoutBackgroundColor() {
        return layoutBackgroundColor;
    }

    public TabLayout getTabLayout() {
        return tabLayout;
    }
}
